package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory

import android.animation.ObjectAnimator
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.lizardmind.haofudoctor.R

/**
 * Created by Administrator on 2017/11/3.
 */
class BottomPullView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var bottomHeight:Int=dp2px(104)
    private var isShow=false

    private lateinit var topView: View
    private lateinit var bottomView : View

    init {
        val array=context.obtainStyledAttributes(attrs, R.styleable.BottomPullView)
        if (array!=null){
            bottomHeight=array.getDimensionPixelOffset(dp2px(R.styleable.BottomPullView_bottomHeight),dp2px(104))
            array.recycle()
        }
    }

    override fun onLayout(boolean: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        for (a in 0 until childCount){
            val view=getChildAt(a)
            when (a) {
                childCount-1 -> view.layout(paddingStart,paddingTop+bottom-bottomHeight,paddingEnd+measuredWidth,
                        paddingBottom+bottom-bottomHeight+measuredHeight)
                0 ->            view.layout(paddingStart,paddingTop,paddingEnd+measuredWidth,paddingBottom+measuredHeight-bottomHeight)
//                else ->         view.layout(paddingStart,paddingTop,right,bottom)
            }

        }
        topView = getChildAt(0)
        bottomView = getChildAt(childCount - 1)
    }


    private fun showBottom(animatorRunTime: Long){
        val valueHeight = bottomView.measuredHeight - bottomHeight
        isShow=true
        ObjectAnimator.ofFloat(topView, "translationY", 0f, -valueHeight.toFloat()).setDuration(animatorRunTime).start()
        ObjectAnimator.ofFloat(bottomView, "translationY", 0f, -valueHeight.toFloat()).setDuration(animatorRunTime).start()
    }
    private fun hideBottom(animatorRunTime: Long){
        val valueHeight = bottomView.measuredHeight - bottomHeight
        isShow=false
        ObjectAnimator.ofFloat(topView, "translationY",-valueHeight.toFloat(),0f).setDuration(animatorRunTime).start()
        ObjectAnimator.ofFloat(bottomView, "translationY",-valueHeight.toFloat(),0f).setDuration(animatorRunTime).start()
    }

    //带弹出或隐藏动画
    fun showAndHide() {
        if (isShow) {
            hideBottom(10)
        } else {
            showBottom(10)
        }
    }

//    //直接弹出最下方view
//    fun showBottomMoment(){
//        showBottom(10)
//    }
//    //直接隐藏最下方view
//    fun hideBottomMoment(){
//        hideBottom(1)
//    }

    fun isShow(): Boolean{
        return isShow
    }

    fun dp2px(dpValue: Int): Int{
        return (dpValue*(context.resources.displayMetrics.density)+0.5f).toInt()
    }

}